package com.house.customer.web;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.house.customer.common.ExtAjaxResponse;
import com.house.customer.common.ExtPageable;
import com.house.customer.entity.Customer;
import com.house.customer.entity.dto.CustomerDTO;
import com.house.customer.service.ICustomerService;


@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private ICustomerService customerService;

//	@RequestMapping(name="/save")
//	public void save() {
//		
//		for(int i=1;i<10;i++) {
//		Customer customer=new Customer();
//		customer.setName("tianmeng"+i);
//		customer.setState("已买");
//		customer.setPhone(i+99999);
//		customer.setArea(100+i);
//		customer.setCheckInTime(new Date());
//		customer.setCommunity("朝阳小区");
//		customer.setEmail("1000@qq.com");
//		customer.setHouseType("三房一厅");
//		customer.setPrice(160000);
//		customer.setRecorder("李四");
//		customer.setRecordTime(new Date());
//		customer.setSex("男");
//		
//		customerService.saveOrUpdate(customer);}
//	}
	
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse deleteCustomer(@RequestParam Long id) {
		try {
			Customer customer=customerService.findOne(id);
			if (customer!=null) {
				customerService.delete(customer);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	
	@RequestMapping("/deleteCustomers")
	public @ResponseBody ExtAjaxResponse deleteCustomers(@RequestParam Long[] ids) {
		try {
			List<Long>idLists=Arrays.asList(ids);
			if (idLists!=null) {
				customerService.delete(idLists);
			}
			return new ExtAjaxResponse(true,"操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"操作失败！");
		}
	}
	
	
	@RequestMapping("/findOne")
	public @ResponseBody Customer findOne(@RequestParam Long id) 
	{
		Customer customer =  customerService.findOne(id);
		
		return customer;
	}
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Customer> findAll() 
	{
		 List<Customer> lists =  customerService.findAll();
		
		return lists;
	}
	
	@RequestMapping("/findPage")
    public @ResponseBody Page<Customer>fingPage(CustomerDTO customerDTO,ExtPageable pageable){
		
		Page<Customer> page=customerService.findAll(CustomerDTO.getSpecification(customerDTO),pageable.getPageable());
    	
		return page;
    }
	
	
	
}
